﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Input;


namespace Beat_em_Up
{
    class HUD
    {
        Texture2D bar_full_01, bar_full_02, bar_full_03, bar_empty_01, bar_empty_02, bar_empty_03;
        Rectangle objLifebarEdge1;
        Rectangle objLifebarEdge2;
        Rectangle objLifebarEdge3;

        Game1 game;
        Vector2[] barEmpty;
        Vector2[] barFull;

        public int currentHP = 5;
        public const int maxHP = 30;

        public HUD(ContentManager Content)
        {
            game = new Game1();
            barEmpty = new Vector2[maxHP];
            barFull = new Vector2[currentHP];
            bar_empty_01 = Content.Load<Texture2D>("HUDImages/lifebarempty1");
            bar_empty_02 = Content.Load<Texture2D>("HUDImages/lifebarempty2");
            bar_empty_03 = Flip.FlipImage(Content.Load<Texture2D>("HUDImages/lifebarempty1"), false, true);

            bar_full_01 = Content.Load<Texture2D>("HUDImages/lifebarfull1");
            bar_full_02 = Content.Load<Texture2D>("HUDImages/lifebarfull2");
            bar_full_03 = Flip.FlipImage(Content.Load<Texture2D>("HUDImages/lifebarfull1"), false, true);

            objLifebarEdge1 = new Rectangle(game.graphics.PreferredBackBufferWidth / 8 - bar_empty_01.Width, game.graphics.PreferredBackBufferHeight / 8 - bar_empty_01.Height, bar_empty_01.Width, bar_empty_01.Height);

            for (int i = 0; i < barEmpty.Length; i++)
            {
                barEmpty[i] = new Vector2(i * objLifebarEdge1.Width + (objLifebarEdge1.X + bar_empty_02.Width), objLifebarEdge1.Y);
                objLifebarEdge3 = new Rectangle((int)barEmpty[i].X + objLifebarEdge1.Width, objLifebarEdge1.Y, bar_empty_02.Width, bar_empty_02.Height);
            }

            for (int i = 0; i < barFull.Length; i++)
            {
                barFull[i] = new Vector2(i * objLifebarEdge1.Width + (objLifebarEdge1.X + bar_empty_02.Width), objLifebarEdge1.Y);
                objLifebarEdge2 = new Rectangle((int)barFull[i].X + objLifebarEdge1.Width, objLifebarEdge1.Y, bar_empty_02.Width, bar_empty_02.Height);
            }
        }

        public void Update(GameTime gameTime, KeyboardState currentKeyboard, KeyboardState oldKeyboard)
        {
            if (currentKeyboard.IsKeyDown(Keys.O) && oldKeyboard.IsKeyUp(Keys.O))
            {

                if (currentHP > 0)
                {
                    currentHP -= 1;
                    Vector2[] foo = new Vector2[(barFull.Length) - 1];
                    for (int i = 0; i < foo.Length; i++)
                    {
                        foo[i] = new Vector2(i * objLifebarEdge1.Width + (objLifebarEdge1.X + bar_empty_02.Width), objLifebarEdge1.Y);
                        objLifebarEdge2 = new Rectangle((int)foo[i].X + objLifebarEdge1.Width, objLifebarEdge1.Y, bar_empty_02.Width, bar_empty_02.Height);
                    }

                    barFull = foo;
                }
            }

            else if (currentKeyboard.IsKeyDown(Keys.P) && oldKeyboard.IsKeyUp(Keys.P))
            {
                if (currentHP < maxHP)
                {
                    currentHP += 1;
                    Vector2[] foo = new Vector2[(barFull.Length) + 1];
                    for (int i = 0; i < foo.Length; i++)
                    {
                        foo[i] = new Vector2(i * objLifebarEdge1.Width + (objLifebarEdge1.X + bar_empty_02.Width), objLifebarEdge1.Y);
                        objLifebarEdge2 = new Rectangle((int)foo[i].X + objLifebarEdge1.Width, objLifebarEdge1.Y, bar_empty_02.Width, bar_empty_02.Height);
                    }

                    barFull = foo;
                }
            }
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            for (int i = 0; i < barEmpty.Length; i++)
            {
                spriteBatch.Draw(bar_empty_02, barEmpty[i], Color.White);
            }
            spriteBatch.Draw(bar_empty_03, objLifebarEdge3, Color.White);
            spriteBatch.Draw(bar_empty_01, objLifebarEdge1, Color.White);

            for (int i = 0; i < barFull.Length; i++)
            {
                spriteBatch.Draw(bar_full_02, barFull[i], Color.White);
            }
    
            if (currentHP > 0)
            {
            spriteBatch.Draw(bar_full_03, objLifebarEdge2, Color.White);
            spriteBatch.Draw(bar_full_01, objLifebarEdge1, Color.White);
            }       
        }
    }
}